<?php

/**
 * 
 * @author yuxun
 *
 */
class ResourceCommand extends CConsoleCommand {

	//  */1 * * * * /usr/local/bin/php /data/web/webdata/oss/crontab.php resource >> /home/webuser/tmp/resource.log
	/**
	 * 资源定时修改状态(non-PHPdoc)
	 * @see CConsoleCommand::run()
	 */
    public function run($args) {
    	set_time_limit (0);
    	echo "............................... \n";
    	echo "资源定时修改状态任务开始 \n";
    	echo "开始时间：".date('Y-m-d H:i:s',time())."\n";
 
        $nowTime = strtotime(date('Y-m-d H:i:00',time()));  //当前时间
        $modResource = new ModResource();
        //查询设置为当前时间的list
        $list = $modResource->findAllByAttributes(array('game'=>'mango','automatic_time'=>$nowTime,'is_automatic'=>1,'is_valid'=>1));
        
        if(count($list) == 0){
        	echo "暂无符合的数据 \n";
        }

        foreach ($list as $key=>$val){
        	$attr = array('is_valid'=> 2);
        	$result = $modResource->updateByPk($val->id, $attr);
        	$automatic_info = '';
        	if(!$result){
        		$automatic_info .= "序号：".$val->id." 的数据修改状态失败; "."\n";
        	}else{
        		// 记录到文件中
        		$out = self::record($val->id);
        		$automatic_info .= $out['msg']."\n";
        	}
        	echo $automatic_info;
        	
        	$update = array();
        	$update['is_automatic'] = 2;
        	$update['automatic_info'] = $automatic_info;
        	$modResource->updateByPk($val->id, $update);
        }

        echo "结束时间：".date('Y-m-d H:i:s',time())."\n";
        echo "资源定时修改状态任务结束 \n";
    }
    
    /**
     * 获取服务器ip地址
     * @return string
     */
    public static function getIp(){
    	$file = '/data/sh/ip_list/eth1';
    	$ip = file_get_contents($file);
    	return $ip;
    }
    
    /**
     * 记录到文件中
     * @param unknown $id
     * @return multitype:number string
     */
    public static function record($id){
    	$info = ResourceService::get($id);
    	if($info !== false){
    		$ip = self::getIp();
    		$ip = trim($ip);
    		if($ip == '127.0.0.1'){
    			$tmp_path = "F:/usr/download/";
    		}else if($ip == '192.168.1.10'){
    			//$tmp_path = '/xujiawen_test/download/';
    			$tmp_path = '/home/webuser/webapp/download/';
    		}else if($ip == '123.58.17.219'){
    			//$tmp_path = '/xujiawen_test/download/';
    			$tmp_path = '/data/games/d1/s10cdn_res/download/';
    		}
    		echo 'tmp_path== '.$tmp_path;	
    		$fp = fopen($tmp_path."upload_log.txt", "a");
    		$str = $info['v_num']." ".$info['s_version']." ".$info['platform']." ".$info['channel']." ".$info['download_path']." ".$info['apk']." ".time()."\r\n";
    		$fnum = fwrite($fp, $str);
    		fclose($fp);
    		if($fnum !== false){
    			$out = array('status'=>1,'msg'=>'任务成功','data'=>'');
    		}else{
    			$out = array('status'=>0,'msg'=>'记录文件失败','data'=>'');
    		}
    
    	}else{
    		$out = array('status'=>0,'msg'=>'非法参数','data'=>'');
    	}
    
    	return $out;
    
    }

   

        
}